/*
 * $Id$
 * 
 * CONFIDENTIAL AND PROPRIETARY. (c) 2007 Revolution Health Group LLC. All rights reserved. 
 * This source code may not be disclosed to others, used or reproduced without the written permission of Revolution Health Group.
 */
package org.javason.jsonrpc;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.apache.commons.beanutils.BeanUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.test.JSONAssert;
import net.sf.json.util.JSONUtils;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.ModelAndView;

import org.javason.SimpleImpl;
import org.javason.ContextInterfaceImpl;
import org.javason.json.BeanToJsonSerializer;


import junit.framework.TestCase;

public class TestJsonRpc extends AbstractJsonRpcTestCase {
	
	public void testGet() {
		System.out.println("RUNNING testGet()");
		String requestURI = "/";
		//MockHttpServletRequest request = new MockHttpServletRequest("GET", requestURI);
		MockHttpServletRequest request = new MockHttpServletRequest("POST", requestURI);
		MockHttpServletResponse response = new MockHttpServletResponse();
		request.setPathInfo(requestURI);
		//System.out.println("request:" + request.getMethod().toString());
		try {
			JSONArray param1 = new JSONArray();
			
			JSONObject blah = JsonRpcRequestFactory.createJsonRpcRequest(47, "getEnumType", param1);
			//SHOULD ERROR OUT - request.setContent(blah.toString().getBytes("UTF-8"));
			request.setContent("".getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			JsonRpcServiceExporter exporter = new JsonRpcServiceExporter();
			exporter.setServiceBean(new JsonInterface());
			
			ModelAndView mv = exporter.handleRequest(request, response);
			
			JsonRpcServiceView iv = new JsonRpcServiceView();
			iv.render(mv.getModel(), request, response);
			System.out.println("Status: " + response.getStatus());
			System.out.println(response.getContentAsString());
			
			JSONObject responseObj = JSONObject.fromObject(response.getContentAsString());

			assertTrue(JSONUtils.isNull(responseObj.opt("result")));
			assertTrue(responseObj.has("error"));
			assertTrue(JSONUtils.isNull(responseObj.opt("id")));
			
		} catch (Exception e1) {
			e1.printStackTrace();
			assertTrue(false);
		}
		System.out.println("--");
	}
	
	public void testException() {
		System.out.println("RUNNING testException()");
		String requestURI = "/";
		MockHttpServletRequest request = new MockHttpServletRequest("POST", requestURI);
		MockHttpServletResponse response = new MockHttpServletResponse();
		request.setPathInfo(requestURI);

		try {
			JSONArray param1 = new JSONArray();
			
			JSONObject blah = JsonRpcRequestFactory.createJsonRpcRequest(47, "testException", param1);
			request.setContent(blah.toString().getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			JsonRpcServiceExporter exporter = new JsonRpcServiceExporter();
			exporter.setServiceBean(new JsonInterface());
			
			ModelAndView mv = exporter.handleRequest(request, response);
			
			JsonRpcServiceView iv = new JsonRpcServiceView();
			iv.render(mv.getModel(), request, response);
			System.out.println("Status: " + response.getStatus());
			System.out.println(response.getContentAsString());
			
			JSONObject responseObj = JSONObject.fromObject(response.getContentAsString());
			System.out.println(responseObj.toString(3));
			assertTrue(JSONUtils.isNull(responseObj.opt("result")));
			assertTrue(responseObj.has("error"));
			assertEquals(99, responseObj.getJSONObject("error").getJSONObject("error").getInt("resultCode"));
			assertTrue(responseObj.has("id"));
			
		} catch (Exception e1) {
			e1.printStackTrace();
			assertTrue(false);
		}
		System.out.println("--");
	}
	
	public void testEnum() {
		System.out.println("RUNNING testEnum()");
		String requestURI = "/world";
		MockHttpServletRequest request = new MockHttpServletRequest("POST", requestURI);
		MockHttpServletResponse response = new MockHttpServletResponse();
		request.setPathInfo(requestURI);
		
		try {
			JSONArray param1 = new JSONArray();
			
			JSONObject blah = JsonRpcRequestFactory.createJsonRpcRequest(47, "getEnumType", param1);
			request.setContent(blah.toString().getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			JsonRpcServiceExporter exporter = new JsonRpcServiceExporter();
			exporter.setServiceBean(new JsonInterface());
			
			ModelAndView mv = exporter.handleRequest(request, response);
			
			JsonRpcServiceView iv = new JsonRpcServiceView();
			iv.render(mv.getModel(), request, response);
			System.out.println("Status: " + response.getStatus());
			System.out.println(response.getContentAsString());
			
			JSONObject responseObj = JSONObject.fromObject(response.getContentAsString());
			assertTrue(responseObj.has("result"));
			assertTrue(JSONUtils.isNull(responseObj.opt("error")));
			assertTrue(responseObj.getInt("id") == 47);
			
		} catch (Exception e1) {
			e1.printStackTrace();
			assertTrue(false);
		}
		System.out.println("--");
	}
	
	public void testJSONRPCService() {
		System.out.println("RUNNING testJSONRPCService()");
		String requestURI = "/world";
		MockHttpServletRequest request = new MockHttpServletRequest("POST", requestURI);
		MockHttpServletResponse response = new MockHttpServletResponse();
		request.setPathInfo(requestURI);
		
		try {
			JSONArray param1 = new JSONArray();
			param1.add(0, "testing123");
			
			JSONObject blah = JsonRpcRequestFactory.createJsonRpcRequest(47, "blah", param1);
			request.setContent(blah.toString().getBytes("UTF-8"));
		} catch (JSONException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			JsonRpcServiceExporter exporter = new JsonRpcServiceExporter();
			exporter.setServiceBean(new ContextInterfaceImpl());
			
			ModelAndView mv = exporter.handleRequest(request, response);
			
			JsonRpcServiceView iv = new JsonRpcServiceView();
			iv.render(mv.getModel(), request, response);
			System.out.println("Status: " + response.getStatus());
			System.out.println(response.getContentAsString());
			
			JSONObject responseObj = JSONObject.fromObject(response.getContentAsString());
			assertTrue(JSONUtils.isNull(responseObj.opt("result")));
			assertTrue(JSONUtils.isNull(responseObj.opt("error")));
			assertTrue(responseObj.getInt("id") == 47);
			
		} catch (Exception e1) {
			e1.printStackTrace();
			assertTrue(false);
		}
		System.out.println("--");
	}
	
	public void testFindWorldContext() {
		System.out.println("RUNNING testFindWorldContext()");
		String requestURI = "/world";
		MockHttpServletRequest request = new MockHttpServletRequest("POST", requestURI);
		MockHttpServletResponse response = new MockHttpServletResponse();
		request.setPathInfo(requestURI);
		
		JSONObject findWorldContext = new JSONObject();
		JSONObject testId = new JSONObject();
		try {
			testId.put("someCtx", false);
			findWorldContext.put("id", testId);
		
			findWorldContext.put("method", "findWorldContext");
			JSONArray param1 = new JSONArray();
			param1.add(0, (int) 99);
			findWorldContext.put("params", param1);
		
			request.setContent(findWorldContext.toString().getBytes("UTF-8"));
		} catch (JSONException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			assertTrue(false);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			assertTrue(false);
		}
		
		try {
			JsonRpcServiceExporter exporter = new JsonRpcServiceExporter();
			exporter.setServiceBean(new ContextInterfaceImpl());
			
			ModelAndView mv = exporter.handleRequest(request, response);
			
			JsonRpcServiceView iv = new JsonRpcServiceView();
			iv.render(mv.getModel(), request, response);
			System.out.println("Status: " + response.getStatus());
			System.out.println(response.getContentAsString());
			
			JSONObject responseObj = JSONObject.fromObject(response.getContentAsString());
			assertTrue(responseObj.has("result"));
			assertTrue(JSONUtils.isNull(responseObj.opt("error")));
			assertTrue(responseObj.getJSONObject("id").toString().equals(testId.toString()));
		} catch (Exception e1) {
			e1.printStackTrace();
			assertTrue(false);
		}
		System.out.println("--");
	}
	
	public void testJsonInterface() {
		System.out.println("RUNNING testJsonInterface()");
		String requestURI = "/world";
		MockHttpServletRequest request = new MockHttpServletRequest("POST", requestURI);
		MockHttpServletResponse response = new MockHttpServletResponse();
		request.setPathInfo(requestURI);
		
		JSONObject testArray = new JSONObject();
		JSONObject testId = new JSONObject();
		try {
			testId.put("someCtx", false);
			testArray.put("id", testId);
		
			testArray.put("method", "testArray");
			JSONArray param1 = new JSONArray();
/*
			JSONArray doubleArray = new JSONArray();
			doubleArray.put(2.1);
			doubleArray.put(4.3);
			double temp = 99.0;
			doubleArray.put(temp);
			
			param1.put(0, doubleArray);
	*/		
			JSONArray intArray = new JSONArray();
			intArray.add(99);
			intArray.add(47);
			param1.add(0, intArray);
			testArray.put("params", param1);
			System.out.println("testArray" + testArray.toString(3));
			request.setContent(testArray.toString().getBytes("UTF-8"));
		} catch (JSONException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			assertTrue(false);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			assertTrue(false);
		}
		
		try {
			JsonRpcServiceExporter exporter = new JsonRpcServiceExporter();
			exporter.setServiceBean(new JsonInterface());
			
			ModelAndView mv = exporter.handleRequest(request, response);
			
			JsonRpcServiceView iv = new JsonRpcServiceView();
			iv.render(mv.getModel(), request, response);
			System.out.println("Status: " + response.getStatus());
			System.out.println(response.getContentAsString());
			
			JSONObject responseObj = JSONObject.fromObject(response.getContentAsString());
			assertTrue(responseObj.has("result"));
			assertTrue(JSONUtils.isNull(responseObj.opt("error")));
			assertTrue(responseObj.getJSONObject("id").toString().equals(testId.toString()));
		
			JSONAssert.assertEquals("{\"result\":\"integerArray =  99 47\",\"error\":null,\"id\":{\"someCtx\":false}}", responseObj);

		} catch (Exception e1) {
			e1.printStackTrace();
			assertTrue(false);
		}
		System.out.println("--");
	}

	
	public void testJsonInterface2() {
		System.out.println("RUNNING testJsonInterface2()");
		String requestURI = "/world";
		MockHttpServletRequest request = new MockHttpServletRequest("POST", requestURI);
		MockHttpServletResponse response = new MockHttpServletResponse();
		request.setPathInfo(requestURI);
		
		JSONObject testArray = new JSONObject();
		JSONObject testId = new JSONObject();
		try {
			testId.put("someCtx", false);
			testArray.put("id", testId);
		
			testArray.put("method", "testArray2");
			JSONArray param1 = new JSONArray();
		
			JSONArray strArray = new JSONArray();
			strArray.add("test1");
			strArray.add("222");
			param1.add(0, strArray);
			testArray.put("params", param1);
		
			request.setContent(testArray.toString().getBytes("UTF-8"));
		} catch (JSONException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			assertTrue(false);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			assertTrue(false);
		}
		
		try {
			JsonRpcServiceExporter exporter = new JsonRpcServiceExporter();
			exporter.setServiceBean(new JsonInterface());
			
			ModelAndView mv = exporter.handleRequest(request, response);
			
			JsonRpcServiceView iv = new JsonRpcServiceView();
			iv.render(mv.getModel(), request, response);
			System.out.println("Status: " + response.getStatus());
			System.out.println(response.getContentAsString());
			
			JSONObject responseObj = JSONObject.fromObject(response.getContentAsString());
			assertTrue(responseObj.has("result"));
			assertTrue(JSONUtils.isNull(responseObj.opt("error")));
			assertTrue(responseObj.getJSONObject("id").toString().equals(testId.toString()));
			JSONAssert.assertEquals(JSONObject.fromObject("{\"result\":\"strArray =  test1 222\",\"error\":null,\"id\":{\"someCtx\":false}}"), responseObj);
		} catch (Exception e1) {
			e1.printStackTrace();
			assertTrue(false);
		}
		System.out.println("--");
	}

	public void testJsonInterface3() {
		System.out.println("RUNNING testJsonInterface3()");
		String requestURI = "/world";
		MockHttpServletRequest request = new MockHttpServletRequest("POST", requestURI);
		MockHttpServletResponse response = new MockHttpServletResponse();
		request.setPathInfo(requestURI);
		
		JSONObject testArray = new JSONObject();
		JSONObject testId = new JSONObject();
		try {
			testId.put("someCtx", false);
			testArray.put("id", testId);
		
			testArray.put("method", "testArray3");
			JSONArray param1 = new JSONArray();
		
			JSONArray beanArray = new JSONArray();
			JsonBean2 bean = new JsonBean2();
			bean.setTestBoolean(true);
			beanArray.add(BeanToJsonSerializer.serializeToJsonObject(bean));
			param1.add(0, beanArray);
			testArray.put("params", param1);
		
			request.setContent(testArray.toString().getBytes("UTF-8"));
		} catch (JSONException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			assertTrue(false);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			assertTrue(false);
		}
		
		try {
			JsonRpcServiceExporter exporter = new JsonRpcServiceExporter();
			exporter.setServiceBean(new JsonInterface());
			
			ModelAndView mv = exporter.handleRequest(request, response);
			
			JsonRpcServiceView iv = new JsonRpcServiceView();
			iv.render(mv.getModel(), request, response);
			System.out.println("Status: " + response.getStatus());
			System.out.println(response.getContentAsString());
			
			JSONObject responseObj = JSONObject.fromObject(response.getContentAsString());
			assertTrue(responseObj.has("result"));
			assertTrue(JSONUtils.isNull(responseObj.opt("error")));
			assertTrue(responseObj.getJSONObject("id").toString().equals(testId.toString()));
			//assertEquals(responseObj.toString(), "{\"result\":\"strArray =  test1 222\",\"error\":null,\"id\":{\"someCtx\":false}}");
		} catch (Exception e1) {
			e1.printStackTrace();
			assertTrue(false);
		}
		System.out.println("--");
	}
	
	
	public void testJsonInterface4() {
		System.out.println("RUNNING testJsonInterface4()");
		String requestURI = "/world";
		MockHttpServletRequest request = new MockHttpServletRequest("POST", requestURI);
		MockHttpServletResponse response = new MockHttpServletResponse();
		request.setPathInfo(requestURI);
		
		JSONObject testArray = new JSONObject();
		JSONObject testId = new JSONObject();
		try {
			JSONObject jsonObject = new JSONObject();
            jsonObject.put("method", "getQuotes");
            jsonObject.put("id", "1");

            JSONArray params = new JSONArray();
            JSONArray customers = new JSONArray();
            params.add("2");

            
            GregorianCalendar gregCal = new GregorianCalendar();
            gregCal.set(1975, 11, 30, 0, 0, 0);
            XMLGregorianCalendar gc = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregCal);

            WsfCustomer wsfCustomer = new WsfCustomer();
            wsfCustomer.setFirstName("hari");
            wsfCustomer.setLastName("pandalai");
            wsfCustomer.setEmailAddress("hpandalai@yahoo.com");
            wsfCustomer.setPhoneNumber("202-746-4103");
            //wsfCustomer.setPhoneType(PhoneTypeList.CELL);
            wsfCustomer.setPhoneIsPreferred(Boolean.TRUE);
            wsfCustomer.setCustomerId(0L);
            wsfCustomer.setAssociatedCampaignId(2);
            //wsfCustomer.setRelationshipType(RelationshipTypeList.PRIMARY);
            wsfCustomer.setDateOfBirth(gc);
            wsfCustomer.setUsesTobacco(Boolean.FALSE);
            //wsfCustomer.setInterestedPlanType(PlanTypeList.IMM);
            customers.add(BeanToJsonSerializer.serializeToJsonObject(wsfCustomer));
            System.out.println("customer JSON " + BeanToJsonSerializer.serializeToJsonObject(wsfCustomer));
            params.add(customers);
            //System.out.println("customers: " + String.valueOf(customer));
            params.add("19301");
            params.add("Chester");
            params.add("2006-12-30T00:00:00.594-05:00");
            jsonObject.put("params", params);
            jsonObject.put("id", testId);
			
			//////////
            System.out.println("params: " + jsonObject.toString(3));
			request.setContent(jsonObject.toString().getBytes("UTF-8"));
		} catch (JSONException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			assertTrue(false);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			assertTrue(false);
		} catch (DatatypeConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			assertTrue(false);
		}
		
		try {
			JsonRpcServiceExporter exporter = new JsonRpcServiceExporter();
			exporter.setServiceBean(new ConverterTestInterface());
			
			ModelAndView mv = exporter.handleRequest(request, response);
			
			JsonRpcServiceView iv = new JsonRpcServiceView();
			iv.render(mv.getModel(), request, response);
			System.out.println("Status: " + response.getStatus());
			System.out.println(response.getContentAsString());
			
			JSONObject responseObj = JSONObject.fromObject(response.getContentAsString());
			assertTrue(responseObj.has("result"));
			assertTrue(JSONUtils.isNull(responseObj.opt("error")));
			assertTrue(responseObj.getJSONObject("id").toString().equals(testId.toString()));
			//assertEquals(responseObj.toString(), "{\"result\":\"strArray =  test1 222\",\"error\":null,\"id\":{\"someCtx\":false}}");
		} catch (Exception e1) {
			e1.printStackTrace();
			assertTrue(false);
		}
		System.out.println("--");
	}
	
	public void testJsonInterface5() {
		System.out.println("RUNNING testJsonInterface5()");
		String requestURI = "/world";
		MockHttpServletRequest request = new MockHttpServletRequest("POST", requestURI);
		MockHttpServletResponse response = new MockHttpServletResponse();
		request.setPathInfo(requestURI);
		
		JSONObject testArray = new JSONObject();
		JSONObject testId = new JSONObject();
		try {
			testId.put("someCtx", false);
			testArray.put("id", testId);
		
			testArray.put("method", "multipleGenerics");
			JSONArray param1 = new JSONArray();
		
			ArrayList<String> names = new ArrayList<String>() {{
				add("testor"); 
			}};
			
			ArrayList<Integer> ids = new ArrayList<Integer>() {{
				add(12); 
			}};
			
			param1.add(0, BeanToJsonSerializer.serialize(names));
			param1.add(1, BeanToJsonSerializer.serialize(ids));
			testArray.put("params", param1);
			System.out.println("params: " + testArray.toString(3));
			request.setContent(testArray.toString().getBytes("UTF-8"));
		} catch (JSONException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			assertTrue(false);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			assertTrue(false);
		}
		
		try {
			JsonRpcServiceExporter exporter = new JsonRpcServiceExporter();
			exporter.setServiceBean(new ConverterTestInterface());
			
			ModelAndView mv = exporter.handleRequest(request, response);
			
			JsonRpcServiceView iv = new JsonRpcServiceView();
			iv.render(mv.getModel(), request, response);
			System.out.println("Status: " + response.getStatus());
			System.out.println(response.getContentAsString());
			
			JSONObject responseObj = JSONObject.fromObject(response.getContentAsString());
			assertTrue(responseObj.has("result"));
			assertTrue(JSONUtils.isNull(responseObj.opt("error")));
			assertTrue(responseObj.getJSONObject("id").toString().equals(testId.toString()));
			//assertEquals(responseObj.toString(), "{\"result\":\"strArray =  test1 222\",\"error\":null,\"id\":{\"someCtx\":false}}");
		} catch (Exception e1) {
			e1.printStackTrace();
			assertTrue(false);
		}
		System.out.println("--");
	}
	
	
	public void testJsonInterface6() {
		System.out.println("RUNNING testJsonInterface6()");
		String requestURI = "/world";
		MockHttpServletRequest request = new MockHttpServletRequest("POST", requestURI);
		MockHttpServletResponse response = new MockHttpServletResponse();
		request.setPathInfo(requestURI);
		
		JSONObject testArray = new JSONObject();
		JSONObject testId = new JSONObject();
		try {
			testId.put("someCtx", false);
			testArray.put("id", testId);
		
			testArray.put("method", "testFloat");
			JSONArray param1 = new JSONArray();
			testArray.put("params", param1);
			System.out.println("params: " + testArray.toString(3));
			request.setContent(testArray.toString().getBytes("UTF-8"));
		} catch (JSONException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			assertTrue(false);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			assertTrue(false);
		}
		
		try {
			JsonRpcServiceExporter exporter = new JsonRpcServiceExporter();
			exporter.setServiceBean(new JsonInterface());
			
			ModelAndView mv = exporter.handleRequest(request, response);
			
			JsonRpcServiceView iv = new JsonRpcServiceView();
			iv.render(mv.getModel(), request, response);
			System.out.println("Status: " + response.getStatus());
			System.out.println(response.getContentAsString());
			
			JSONObject responseObj = JSONObject.fromObject(response.getContentAsString());
			assertTrue(responseObj.has("result"));
			assertTrue(JSONUtils.isNull(responseObj.opt("error")));
			assertTrue(responseObj.getJSONObject("id").toString().equals(testId.toString()));
			//assertEquals(responseObj.toString(), "{\"result\":\"strArray =  test1 222\",\"error\":null,\"id\":{\"someCtx\":false}}");
		} catch (Exception e1) {
			e1.printStackTrace();
			assertTrue(false);
		}
		System.out.println("--");
	}
	
	public void testJsonInterface7() {
		System.out.println("RUNNING testJsonInterface7()");
		String requestURI = "/world";
		MockHttpServletRequest request = new MockHttpServletRequest("POST", requestURI);
		MockHttpServletResponse response = new MockHttpServletResponse();
		request.setPathInfo(requestURI);
		
		JSONObject testArray = new JSONObject();
		JSONObject testId = new JSONObject();
		try {
			testId.put("someCtx", false);
			testArray.put("id", testId);
		
			testArray.put("method", "someTestMethod");
			JSONArray param1 = new JSONArray();
			param1.add(true);
			testArray.put("params", param1);
			System.out.println("params: " + testArray.toString(3));
			request.setContent(testArray.toString().getBytes("UTF-8"));
		} catch (JSONException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			assertTrue(false);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			assertTrue(false);
		}
		
		try {
			JsonRpcServiceExporter exporter = new JsonRpcServiceExporter();
			exporter.setServiceBean(new JsonReturningJsonBean());
			
			ModelAndView mv = exporter.handleRequest(request, response);
			
			JsonRpcServiceView iv = new JsonRpcServiceView();
			iv.render(mv.getModel(), request, response);
			System.out.println("Status: " + response.getStatus());
			System.out.println(response.getContentAsString());
			
			JSONObject responseObj = JSONObject.fromObject(response.getContentAsString());
			assertTrue(responseObj.has("result"));
			assertTrue(JSONUtils.isNull(responseObj.opt("error")));
			assertTrue(responseObj.getJSONObject("id").toString().equals(testId.toString()));
			assertTrue(responseObj.getJSONObject("result").getBoolean("testBoolean"));
		} catch (Exception e1) {
			e1.printStackTrace();
			assertTrue(false);
		}
		System.out.println("--");
	}
	
	public void testJsonInterface8() {
		System.out.println("RUNNING testJsonInterface8()");
		
		try {
			String[] testInput = new String[] { "hi", "there" };
			JSONObject responseObj = callMethod(new ArrayService(), "testArrayIn", Arrays.asList(testInput));
			
			
			assertTrue(responseObj.has("result"));
			assertTrue(JSONUtils.isNull(responseObj.opt("error")));
			assertEquals(99, responseObj.getJSONArray("result").get(0));
		} catch (Exception e1) {
			e1.printStackTrace();
			assertTrue(false);
		}
		System.out.println("--");
	}

	/*
	 *  {"id":1,"method":"cancel","params":{"orderId":"1635","customer":{"guid":"262654ac-f3bd-1029-86f6-e95e1a68ba6d","type":null},"cancelReason":null,"credentials":{"username":"RHG1","password":"RHG"}}}
	 */
	
	public void testJsonInterface9() {
		System.out.println("RUNNING testJsonInterface9()");
		
		try {
			String jsonString = "{\"orderId\":\"1635\",\"customer\":{\"guid\":\"262654ac-f3bd-1029-86f6-e95e1a68ba6d\",\"type\":null},\"cancelReason\":null,\"credentials\":{\"username\":\"RHG1\",\"password\":\"RHG\"}}";
			JSONObject params = JSONObject.fromObject(jsonString);
			
			JSONObject responseObj = callMethod(new DefaultParamService(), "cancel", params);
			
			
			assertTrue(responseObj.has("result"));
			assertTrue(JSONUtils.isNull(responseObj.opt("error")));
			assertTrue(JSONUtils.isNull(responseObj.opt("result")));
		} catch (Exception e1) {
			e1.printStackTrace();
			assertTrue(false);
		}
		System.out.println("--");
	}

	public void testJsonInterfaceWithMap() {
		System.out.println("RUNNING testJsonInterfaceWithMap()");
		
		try {
			List<String> listParam = new ArrayList<String>();
			listParam.add("list item");
			Map<String, String> mapParam = new HashMap<String, String>();
			mapParam.put("something", "here");
			
			JSONObject responseObj = callMethod(new AnnotatedJsonInterfaceWithMap(), "theMinimal", mapParam, listParam);
			
			
			assertTrue(responseObj.has("result"));
			assertTrue(JSONUtils.isNull(responseObj.opt("error")));
			assertTrue(responseObj.getBoolean("result"));
		} catch (Exception e1) {
			e1.printStackTrace();
			assertTrue(false);
		}
		System.out.println("--");
	}
	
	public void testJsonInterfaceWithMap2() {
		System.out.println("RUNNING testJsonInterfaceWithMap2()");
		
		try {
			List<String> listParam = new ArrayList<String>();
			listParam.add("list item");
			Map<String, Boolean> mapParam = new HashMap<String, Boolean>();
			mapParam.put("something", true);
			
			JSONObject responseObj = callMethod(new AnnotatedJsonInterfaceWithMap(), "theMinimal2", mapParam, listParam);
			
			
			assertTrue(responseObj.has("result"));
			assertTrue(JSONUtils.isNull(responseObj.opt("error")));
			assertTrue(responseObj.getBoolean("result"));
		} catch (Exception e1) {
			e1.printStackTrace();
			assertTrue(false);
		}
		System.out.println("--");
	}
	
	public void testJsonInterfaceWithMap3() {
		System.out.println("RUNNING testJsonInterfaceWithMap3()");
		
		try {
			List<String> listParam = new ArrayList<String>();
			listParam.add("list item");
			Map<String, JsonBean> mapParam = new HashMap<String, JsonBean>();
			JsonBean value = new JsonBean();
			value.setTest("test string");
			value.setTestLng(1L);
			mapParam.put("something", value);
			
			JSONObject responseObj = callMethod(new AnnotatedJsonInterfaceWithMap(), "theMinimal3", mapParam, listParam);
			
			
			assertTrue(responseObj.has("result"));
			assertTrue(JSONUtils.isNull(responseObj.opt("error")));
			assertTrue(responseObj.getBoolean("result"));
		} catch (Exception e1) {
			e1.printStackTrace();
			assertTrue(false);
		}
		System.out.println("--");
	}
}
